package cn.dgut.training.controller;

import cn.dgut.training.dto.AdDTO;
import cn.dgut.training.entity.activity.Carousel;
import cn.dgut.training.mapper.CarouselMapper;
import cn.dgut.training.service.ICarouselService;
import cn.dgut.training.service.impl.CarouselServiceImpl;
import cn.dgut.training.vo.CommonResponse;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.time.LocalDate;
import java.util.List;

@RestController
@RequestMapping("/ad")
public class CarouselController {
    @Resource
    private ICarouselService iCarouselService;
    @Resource
    private CarouselMapper carouselMapper;
    @Resource
    private CarouselServiceImpl carouselService;


    @PostMapping("/list")
    public CommonResponse pageAd(@RequestBody AdDTO adDTO) {
        return CommonResponse.success(carouselService.listCouponByPage(adDTO));
    }

    @GetMapping("/front/list")
    public List frontListCarousel() {
        QueryWrapper queryWrapper = new QueryWrapper();
        LocalDate localDate = LocalDate.now();
        queryWrapper.le("start_time", localDate);
        queryWrapper.ge("end_time", localDate);
        queryWrapper.eq("enabled", 1);
        queryWrapper.select("position,goods_id,url");
        queryWrapper.orderBy(true, true, "position");
        queryWrapper.last("limit 3");
        return carouselService.list(queryWrapper);
    }

    //创建ad
    @PostMapping("/create")
    public CommonResponse allAd(@RequestBody Carousel carousel) {
        iCarouselService.save(carousel);
        return CommonResponse.message();
    }

    @PostMapping("/update")
    public CommonResponse updateAd(@RequestBody Carousel carousel) {
        iCarouselService.saveOrUpdate(carousel);
        return CommonResponse.success(carousel);
    }

    @PostMapping("/delete")
    public CommonResponse deleteAd(@RequestBody Carousel carousel) {
        iCarouselService.removeById(carousel.getId());
        return CommonResponse.success(carousel);
    }
}
